//
//  GQADViewController.m
//  MyMusic
//
//  Created by sunny on 16/4/15.
//  Copyright © 2016年 sunny. All rights reserved.
//

#import "GQADViewController.h"
#import <AFHTTPSessionManager.h>
#import <MJExtension.h>
#import <UIImageView+WebCache.h>
#import "GQAdItem.h"
#import "GQTabBarController.h"

#define iPhone6P GQUIScreenH == 736
#define iPhone6 GQUIScreenH == 667
#define iPhone5 GQUIScreenH == 568
#define iPhone4 GQUIScreenH == 480
@interface GQADViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *launImageView;

@property (nonatomic,weak)UIImageView *adImageView;
@property (weak, nonatomic) IBOutlet UIButton *jumpButton;
@property (nonatomic, strong)NSTimer *timer;

@end

@implementation GQADViewController

- (UIImageView *)adImageView
{
    if (_adImageView == nil) {
        UIImageView *adImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
        _adImageView = adImageView;
        [self.view insertSubview:adImageView atIndex:1];
    }
    return _adImageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    设置启动图片
    [self setupLaunchImage];
    
//    发送请求，设置图片
    [self loadData];
    
//        开启定时器。进入主框架
    [self startTimer];
    
}

- (void)setupLaunchImage
{
    
//    给不同的屏幕设置不同的图片(屏幕适配)
    if (iPhone6P)
        self.launImageView.image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
    else if (iPhone6)
        self.launImageView.image = [UIImage imageNamed:@"LaunchImage-800-667h@2x"];
     else if (iPhone5)
         self.launImageView.image = [UIImage imageNamed:@"LaunchImage-700-568h@2x"];
}

#pragma mark - 发送请求，设置图片
- (void)loadData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    [manager GET:@"http://g1.163.com/preloadimg?app=A1D6253F&platform=ios&netstat=0&md5=720c445891bcfdb1951825db3cd2b146" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        NSDictionary *dict = responseObject[@"imgs"][0];
        GQAdItem *item = [GQAdItem mj_objectWithKeyValues:dict];
        //       获取图片url
        NSString *urlStr = item.url[0][0];
        //        设置广告图片
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self.adImageView sd_setImageWithURL:[NSURL URLWithString:urlStr]];
        });
        
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
        
    }];
    
}


#pragma mark - 定时器事件
//开启定时器
- (void)startTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeEvent) userInfo:nil repeats:YES];

}
//停止定时器
- (void)stopTimer
{
    [self.timer invalidate];
    self.timer = nil;
}

static int i = 3;
- (void)timeEvent
{
    [self.jumpButton setTitle:[NSString stringWithFormat:@"跳过(%zd秒)",i] forState:UIControlStateNormal];
    
    if (i < 0) {
        i = 0;
        [self jumpBtnClcik];
    }
    
    i--;
    
}
- (IBAction)jumpBtnClcik {

//    进入主框架
    
    GQTabBarController *tabBarVc = [[GQTabBarController alloc] init];
    [UIApplication sharedApplication].keyWindow.rootViewController = tabBarVc;
    [self stopTimer];
}
@end
